class Player {
  constructor() {
    // initsuccess
    this.events = {};
    this.isInitSuccess = false;
    this.timer = null;
    this.time = 10; 
  }
  init() {
    setTimeout(() => { // 模仿耗时操作,假设两秒钟之后，播放器初始化完成
      this.isInitSuccess = true;
      this.emit('initsuccess', '')
    }, 2000);
  }
  play() {
    if (!this.isInitSuccess) throw Error('播放器为初始化')
    // bofang 
    this.time = 10;
    this.timer = setInterval(() => {
       this.time--;
       if (this.time===0){
         clearInterval(this.timer) 
         this.emit('playiend','')
       }
       this.emit('playing', this.time) 
    }, 1000);
  }
  on(eventNamve, cb) {
    this.events[eventNamve] = (this.events[eventNamve] ? this.events[eventNamve].push(cb) : [cb]);
  }
  emit(eventNamve, ...args) {
    (this.events[eventNamve] || []).forEach(eventCallBack => {
      eventCallBack(...args)
    });
  }
}
let player = new Player()
player.init();
player.on('initsuccess', () => {
  console.log('初始化完成')
  player.play()
});
player.on('playing', (time) => {
  console.log('剩余播放时间',time)
})
player.on('playend', (time) => {
  console.log('播放完成')
})
